<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\DB;

class Suggest
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $where[] = ['uid','=',$request->user()->id];
        $start_time = strtotime(date( "Y-m-d 00:00:00"));
        $end_time = strtotime(date( "Y-m-d 23:59:59"));
        $where[] = ['time','>=',$start_time];
        $where[] = ['time','<=',$end_time];
        $count = Db::table('suggest')->where($where)->count();

        if($count>=1)return response()->json(['message' => '每天只能发一条建议!','code'=>'-1']);
        return $next($request);
    }
}
